<?php
require('functions/page_load.php');

$jobs_nav = true;
$active_tab = 'details';

$results = ($_GET['new'] == 'true') ? '<div class="success_result">Job added successfully!</div>' : '' ;

if(isset($_GET['delete_question'])){

	$sql = "DELETE FROM questions WHERE id = '".mysql_real_escape_string($_GET['delete_question'])."'";
	$query = mysql_query( $sql ) or die ("Error in query: $sql. " . mysql_error());
	$results = '<div class="success_result">Question deleted successfully!</div>';
	$active_tab = 'questions';

}

if(isset($_GET['delete_quote'])){

	$sql = "DELETE FROM quotes WHERE id = '".mysql_real_escape_string($_GET['delete_quote'])."'";
	$query = mysql_query( $sql ) or die ("Error in query: $sql. " . mysql_error());
	$results = '<div class="success_result">Quote deleted successfully!</div>';
	$active_tab = 'quotes';

}

if($_SERVER['REQUEST_METHOD'] == 'POST'){

	if($_POST['action'] == 'update_job'){

	$sql = "UPDATE jobs SET 
			area_id = '".mysql_real_escape_string($_POST['area_id'])."', 
			trade_id = '".mysql_real_escape_string($_POST['trade_id'])."', 
			title = '".mysql_real_escape_string($_POST['title'])."', 
			description = '".mysql_real_escape_string($_POST['description'])."', 
			status = '".mysql_real_escape_string($_POST['status'])."', 
			job_active = '".mysql_real_escape_string($_POST['job_active'])."', 
			materials = '".mysql_real_escape_string($_POST['materials'])."' 
			WHERE id = '".mysql_real_escape_string($_GET['id'])."'";
	$query = mysql_query( $sql ) or die ("Error in query: $sql. " . mysql_error());
	
	// update address
	
	$sql = "UPDATE addresses SET 
			area_id = '".mysql_real_escape_string($_POST['area_id'])."', 
			line_1 = '".mysql_real_escape_string($_POST['line_1'])."', 
			line_2 = '".mysql_real_escape_string($_POST['line_2'])."', 
			line_3 = '".mysql_real_escape_string($_POST['line_3'])."', 
			postcode = '".mysql_real_escape_string($_POST['postcode'])."' 
			WHERE id = '".mysql_real_escape_string($_POST['address_id'])."'";
	$query = mysql_query( $sql ) or die ("Error in query: $sql. " . mysql_error());

	$results = '<div class="success_result">Job updated successfully!</div>';

	}

}

$sql = "SELECT addresses.area_id, addresses.id AS address_id, trade_id, title, description, status, job_active, materials, jobs.user_id, line_1, line_2, line_3, postcode 
		FROM jobs 
		LEFT JOIN addresses ON jobs.address_id = addresses.id 
		WHERE jobs.id = '".mysql_real_escape_string($_GET['id'])."'";
$query = mysql_query( $sql ) or die ("Error in query: $sql. " . mysql_error());
$rs = mysql_fetch_assoc($query);

require('includes/header.php');
?>

<div class="side_container">
<h2 class="container_header">Filters</h2>

<ul>
<li><a href="jobs.php">Active jobs</a></li>
<li><a href="jobs.php?show=inactive">Jobs awaiting moderation</a></li>
<li><a href="jobs.php?show=completed">Completed jobs</a></li>
<li><a href="jobs.php?show=cancelled">Cancelled jobs</a></li>
<li><a href="jobs.php?show=deleted">Deleted jobs</a></li>
</ul>

</div>

<div class="main_container_wrapper">

	<div class="main_container">
	<h2 class="container_header">Edit Job</h2>
	
	<div id="top_links">
	<a href="edit_user.php?id=<?php echo $rs['user_id']; ?>" style="background-image: url(images/user.png); padding-left: 20px;">View user</a>
	<a class="tab_link" rel="#questions" style="background-image: url(images/help.png); padding-left: 20px;">Job Questions</a>
	<a class="tab_link" rel="#quotes" style="background-image: url(images/money.png); padding-left: 20px;">Job Quotes</a>
	<a class="tab_link" rel="#details" style="background-image: url(images/page.png); padding-left: 20px;">Job details</a>
	<a href="jobs.php" style="background-image: url(images/back.png); padding-left: 20px;">Back to Jobs</a>
	</div>
	
	<?php echo $results; ?>
	
		<div id="details" class="tab_container"<?php echo ($active_tab == 'details') ? ' style="display: block;"' : ' style="display: none;"' ; ?>>
		
			<form method="post" action="">
			
			<input type="hidden" name="action" value="update_job" />
			<input type="hidden" name="address_id" value="<?php echo $rs['address_id']; ?>" />

			<div class="form_row_wrapper">
			<div class="form_row_left"><label for="title">Title:</label></div>
			<div class="form_row_right"><input type="text" name="title" id="title" class="textbox" value="<?php echo $rs['title']; ?>" style="width: 520px;" /></div>
			</div>
			
			<div class="form_row_wrapper">
			<div class="form_row_left"><label for="description">Description:</label></div>
			<div class="form_row_right"><textarea name="description" id="description" rows="6" cols="30" class="textbox" style="width: 520px; height: 200px;"><?php echo $rs['description']; ?></textarea></div>
			</div>
			
			<div class="form_row_wrapper">
			<div class="form_row_left"><label for="trade_id">Trade:</label></div>
			<div class="form_row_right"><select name="trade_id" id="trade_id">
			<?php
			
			$sql = "SELECT id, trade FROM trades ORDER BY trade";
			$area_query = mysql_query( $sql ) or die ("Error in query: $sql. " . mysql_error());
			while($trade_rs = mysql_fetch_assoc($area_query)){
			
			$selected = ($trade_rs['id'] == $rs['trade_id']) ? ' selected="selected"' : '' ;
			echo '<option value="'.$trade_rs['id'].'"'.$selected.'>'.$trade_rs['trade'].'</option>';
			
			}
			
			?>
			</select></div>
			</div>
			
			<div class="form_row_wrapper">
			<div class="form_row_left"><label for="materials">Materials supplied:</label></div>
			<div class="form_row_right"><input type="checkbox" name="materials" id="materials" value="1"<?php echo ($rs['materials'] == '1') ? ' checked="checked"' : '' ; ?> /></div>
			</div>
			
			<div class="form_row_wrapper">
			<div class="form_row_left"><label for="line_1">Address:</label></div>
			<div class="form_row_right"><span>Editing this address, will edit the users address throughout the system.</span><br />
			<input type="text" name="line_1" id="line_1" class="textbox" value="<?php echo $rs['line_1']; ?>" />
			<input type="text" name="line_2" id="line_2" class="textbox" value="<?php echo $rs['line_2']; ?>" />
			<input type="text" name="line_3" id="line_3" class="textbox" value="<?php echo $rs['line_3']; ?>" />
			<select name="area_id" id="area_id" style="margin-top: 5px;" class="textbox clearLeft">
			<?php
			
			$sql = "SELECT id, area FROM areas ORDER BY area";
			$area_query = mysql_query( $sql ) or die ("Error in query: $sql. " . mysql_error());
			while($area_rs = mysql_fetch_assoc($area_query)){
			
			$selected = ($area_rs['id'] == $rs['area_id']) ? ' selected="selected"' : '' ;
			echo '<option value="'.$area_rs['id'].'"'.$selected.'>'.$area_rs['area'].'</option>';
			
			}
			
			?></select>
			<input type="text" name="postcode" id="postcode" class="textbox clearLeft" value="<?php echo $rs['postcode']; ?>" style="width: 80px;" />
			</div>
			</div>
			
			<div class="form_row_wrapper">
			<div class="form_row_left"><label for="status">Status:</label></div>
			<div class="form_row_right"><select name="status" id="status">
			<option value="Pending"<?php echo ($rs['status'] == 'Pending') ? ' selected="selected"' : '' ; ?>>Pending</option>
			<option value="Closed"<?php echo ($rs['status'] == 'Closed') ? ' selected="selected"' : '' ; ?>>Closed</option>
			<option value="Open"<?php echo ($rs['status'] == 'Open') ? ' selected="selected"' : '' ; ?>>Open</option>
			</select></div>
			</div>
			
			<div class="form_row_wrapper">
			<div class="form_row_left"><label for="job_active">Job active:</label></div>
			<div class="form_row_right"><input type="checkbox" name="job_active" id="job_active" value="1"<?php echo ($rs['job_active'] == '1') ? ' checked="checked"' : '' ; ?> /></div>
			</div>
			
			<div class="form_row_wrapper">
			<div class="form_row_left">&nbsp;</div>
			<div class="form_row_right"><input type="image" src="images/save_button.png" alt="Save" /></div>
			</div>
			
			</form>
		
		</div>
		
		<div id="quotes" class="tab_container"<?php echo ($active_tab == 'quotes') ? ' style="display: block;"' : ' style="display: none;"' ; ?>>
		
		<table width="100%" cellpadding="0" cellspacing="0" class="cms_table">
		<thead><tr><th align="left">Tradesman</th><th>Quote</th><th>Materials</th><th>Date</th><th align="left">Notes</th><th width="50">Delete</th></tr></thead>
		<tbody>
		<?php
		
		$sql = "SELECT quotes.id, quotes.tradesman_id, quote, status, quotes.vat, materials, info, date_quoted, company_name, tradesman.user_id FROM quotes 
				LEFT JOIN tradesman ON quotes.tradesman_id = tradesman.id 
				WHERE job_id = '".mysql_real_escape_string($_GET['id'])."'";
		$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
		$rows = mysql_num_rows($query);
		
		if($rows > 0){
		
			while($rs=mysql_fetch_assoc($query)){
			
			$vat = ($rs['vat'] == '1') ? ' inc vat' : ' ex vat' ;

			echo '<tr><td align="left" valign="top"><a href="edit_tradesman.php?id='.$rs['tradesman_id'].'">'.$rs['company_name'].'</a></td><td valign="top">&pound;'.$rs['quote'].$vat.'</td><td valign="top"><img src="images/'.$rs['materials'].'.png" alt="" /></td><td valign="top">'.date("d/m/Y", strtotime($rs['date_quoted'])).'</td><td align="left">'.$rs['info'].'</td><td valign="top"><a onclick="confirm_delete(\'?id='.$_GET['id'].'&amp;delete_quote='.$rs['id'].'\',\'this quote\');" title="Delete"><img src="images/delete.png" alt="Delete" /></a></td></tr>';
			
			}
			
		}
		
		else{
		
		echo '<tr><td colspan="7">There are currently no quotes for this job.</td></tr>';
		
		}
		
		?>
		
		</tbody></table>
		
		</div>
		
		<div id="questions" class="tab_container"<?php echo ($active_tab == 'questions') ? ' style="display: block;"' : ' style="display: none;"' ; ?>>
		
		
		<table width="100%" cellpadding="0" cellspacing="0" class="cms_table">
		<thead><tr><th align="left">Tradesman</th><th align="left">Question</th><th align="left">Answer</th><th width="50">Delete</th></tr></thead>
		<tbody>
		<?php
		
		$sql = "SELECT questions.id, question, company_name, tradesman.user_id, answer, tradesman_id FROM questions 
				LEFT JOIN answers ON questions.id = answers.question_id 
				LEFT JOIN tradesman ON questions.tradesman_id = tradesman.id 
				WHERE job_id = '".mysql_real_escape_string($_GET['id'])."'";
		$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
		$rows = mysql_num_rows($query);
		
		if($rows > 0){
		
			while($rs=mysql_fetch_assoc($query)){
			
			echo '<tr><td align="left" valign="top"><a href="edit_tradesman.php?id='.$rs['tradesman_id'].'">'.$rs['company_name'].'</a></td><td valign="top" align="left">'.$rs['question'].'</td><td valign="top" align="left">'.$rs['answer'].'</td><td valign="top"><a onclick="confirm_delete(\'?id='.$_GET['id'].'&amp;delete_question='.$rs['id'].'\',\'this question\');" title="Delete"><img src="images/delete.png" alt="Delete" /></a></td></tr>';
			
			}
			
		}
		
		else{
		
		echo '<tr><td colspan="7">There are currently no questions for this job.</td></tr>';
		
		}
		
		?>
		
		</tbody></table>
		
		
		</div>

	</div>

</div>
	
<?php
	require('includes/footer.php');
?>